package com.jdz.gxq.info.admin.enterprise.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.enterprise.bo.Enterprise;
import com.jdz.gxq.info.admin.enterprise.form.AddEnterpriseForm;
import com.jdz.gxq.info.admin.enterprise.service.EnterpriseService;
import com.jdz.gxq.info.common.action.CommonAction;

public class AddEnterpriseAction extends CommonAction {

	private EnterpriseService enterpriseService = null;
	 
	public ActionForward doExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		AddEnterpriseForm addEnterpriseFrom = (AddEnterpriseForm) form;
		
		Enterprise enterprise = addEnterpriseFrom.getEnterprise();
		
		// enterprise名重复验证(其他后台逻辑验证也类似处理)
		if (!enterpriseService.checkEnterpriseName(enterprise, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("enterprise.enterpriseName", new ActionMessage("error.enterpriseNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		enterprise.setModifiedBy(getUserId(request));
		
		enterpriseService.insertEnterprise(enterprise);
		
		return mapping.findForward("success");
	}

	public void setEnterpriseService(EnterpriseService enterpriseService) {
		this.enterpriseService = enterpriseService;
	}
}

